#!/bin/bash

# 定义节点 IP 数组，这里仅用于部分节点判断逻辑，每台虚拟机单独执行时用自身 IP 判断
MASTER_IPS=("192.168.1.101" "192.168.1.102" "192.168.1.103")
WORKER_IPS=("192.168.1.111" "192.168.1.112" "192.168.1.113" "192.168.1.114" "192.168.1.115" "192.168.1.116")
DNS_SERVERS=("8.8.8.8" "114.114.114.114")
K8S_VERSION="1.25.0-00"  # 根据需要修改版本
LOAD_BALANCER_IP="192.168.1.100"  # 负载均衡器 IP
LOAD_BALANCER_PORT="6443"

# 函数：关闭虚拟内存
disable_swap() {
    sudo swapoff -a
    sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
}

# 函数：设置 DNS
set_dns() {
    for dns in "${DNS_SERVERS[@]}"; do
        sudo sed -i "/nameserver/s/^/#/" /etc/resolv.conf
        echo "nameserver $dns" | sudo tee -a /etc/resolv.conf
    done
}

# 函数：安装必要的依赖
install_dependencies() {
    sudo sed -i 's@http://.*archive.ubuntu.com@https://mirrors.aliyun.com@g' /etc/apt/sources.list
    sudo sed -i 's@http://.*security.ubuntu.com@https://mirrors.aliyun.com@g' /etc/apt/sources.list
    sudo apt-get update
    sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common
}

# 函数：安装 containerd
install_containerd() {
    sudo sed -i 's@http://.*archive.ubuntu.com@https://mirrors.aliyun.com@g' /etc/apt/sources.list
    sudo sed -i 's@http://.*security.ubuntu.com@https://mirrors.aliyun.com@g' /etc/apt/sources.list
    sudo apt-get update
    sudo apt-get install -y containerd
    sudo mkdir -p /etc/containerd
    containerd config default | sudo tee /etc/containerd/config.toml
    sudo systemctl restart containerd
}

# 函数：安装 Kubernetes
install_k8s() {
    curl -s https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add -
    echo "deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
    sudo apt-get update
    sudo apt-get install -y kubelet=$K8S_VERSION kubeadm=$K8S_VERSION kubectl=$K8S_VERSION
    sudo apt-mark hold kubelet kubeadm kubectl
}

# 函数：初始化第一个 Master 节点
init_first_master() {
    sudo kubeadm init --control-plane-endpoint "$LOAD_BALANCER_IP:$LOAD_BALANCER_PORT" --pod-network-cidr=10.244.0.0/16
    mkdir -p $HOME/.kube
    sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
    sudo chown $(id -u):$(id -g) $HOME/.kube/config
    kubectl apply -f https://gitee.com/liuqin2014/k8s-shell/raw/master/kube-flannel.yml
}

# 函数：加入 Master 节点
join_master() {
    local JOIN_COMMAND=""
    while [ -z "$JOIN_COMMAND" ]; do
        JOIN_COMMAND=$(ssh "${MASTER_IPS[0]}" "sudo kubeadm token create --print-join-command")
        sleep 5
    done
    CERT_KEY=$(ssh "${MASTER_IPS[0]}" "sudo kubeadm init phase upload-certs --upload-certs | tail -n 1")
    sudo $JOIN_COMMAND --control-plane --certificate-key $CERT_KEY
}

# 函数：加入 Worker 节点
join_worker() {
    local JOIN_COMMAND=""
    while [ -z "$JOIN_COMMAND" ]; do
        JOIN_COMMAND=$(ssh "${MASTER_IPS[0]}" "sudo kubeadm token create --print-join-command")
        sleep 5
    done
    sudo $JOIN_COMMAND
}

# 执行公共操作
disable_swap
set_dns
install_dependencies
install_containerd
install_k8s

# 判断当前节点类型并执行相应操作
CURRENT_IP=$(hostname -I | awk '{print $1}')
if [[ " ${MASTER_IPS[@]} " =~ " ${CURRENT_IP} " ]]; then
    echo "当前节点是 Master"
    if [ "$CURRENT_IP" == "${MASTER_IPS[0]}" ]; then
        init_first_master
    else
        join_master
    fi
elif [[ " ${WORKER_IPS[@]} " =~ " ${CURRENT_IP} " ]]; then
    echo "当前节点是 Worker"
    join_worker
fi

echo "Kubernetes 相关操作执行完成！"